/*
 * @Date         : 2024-05-17 15:58:44 星期5
 * @Author       : xut
 * @Description  :
 */
import { resolve } from "node:path"
import { promisify } from "node:util"
import * as grpc from "@grpc/grpc-js"
import * as protoLoader from "@grpc/proto-loader"
import { ProtoGrpcType } from "../pb/index.services"
import { envConfig } from "../server/config/default"
import nodemailer from "nodemailer"

const options: protoLoader.Options = {
  keepCase: true,
  longs: String,
  enums: String,
  defaults: true,
  oneofs: true,
}
const PORT = envConfig.port
const PROTO_FILE = "../proto/services.proto"
const packageDef = protoLoader.loadSync(resolve(__dirname, PROTO_FILE), options)
const proto = grpc.loadPackageDefinition(packageDef) as unknown as ProtoGrpcType

const client = new proto.auth.AuthService(
  `localhost:${PORT}`,
  grpc.credentials.createInsecure()
)

const deadline = new Date()
deadline.setSeconds(deadline.getSeconds() + 5)

client.waitForReady(deadline, (err) => {
  if (err) {
    console.error(err)
    return
  }

  onClientReady()
})

function onClientReady() {
  // signUpUserCall()
  // signInUserCall()
  // refreshTokenCall()
  // getAuthenticateUserCall()
  verifyEmailCall()
}

// 回调形式调用
function signUpUserCall() {
  client.SignUpUser(
    {
      name: "test mail",
      email: "admin@admin.com",
      password: "password@123",
      passwordConfirm: "password@123",
      photo: "default.png",
    },
    (err, res) => {
      if (err) {
        console.error(err)
        return
      }
      console.log("signUpUser response >>>>", res)
    }
  )
}

// Promise 结合 async / await
const signInUser = promisify(client.signInUser).bind(client)
const refreshToken = promisify(client.refreshToken).bind(client)
const getAuthenticateUser = promisify(client.getMe).bind(client)
const verifyEmail = promisify(client.verifyEmail).bind(client)

async function signInUserCall() {
  try {
    const res = await signInUser({
      email: "admin@admin.com",
      password: "password@123",
    })
    console.log("signInUser response >>>", res)
  } catch (error) {
    console.error(error)
  }
}

async function refreshTokenCall() {
  try {
    const res = await refreshToken({
      refresh_token:
        "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxZjY0NTQ4MC0xOWRmLTRjMWMtYjI2OC04OGI3ZjU2OGM1ZWUiLCJpYXQiOjE3MTYxNjY5NDQsImV4cCI6MTcxNjE3MDU0NH0.b61d_AfbVHEQATe39Wn3YQ__AGwIxapFgatgzolI4yv-UDicCjJPswQZe51uS1JNEuqfgN6D1zDYKIfNaL_qw3kufAsUp479-9tRWwLI6h2HVXf96098wajYsZbPwp08FDKjtjgXfWlGUcWymq7r_NTII1t1b3L7qAhSI9S8cKalMh0lemWBhvawELxJNRHO_xFZqE1v8S_fuqtBtJHryxGQRw97GCoWQ6DcW0x9u0NYeowHH9wfZLs0SNty_Z6XIaocliDLyUsUOmaiQKTcq5N4n254n9crPLX-hMeHnP6Lg7e-7jrPIv5kgrMe-srah9Ldpu-5slQ3kJ2VRTGd9Q",
    })
    console.log("refreshToken response >>>", res)
  } catch (error) {
    console.error(error)
  }
}

async function getAuthenticateUserCall() {
  try {
    const res = await getAuthenticateUser({
      access_token:
        "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxZjY0NTQ4MC0xOWRmLTRjMWMtYjI2OC04OGI3ZjU2OGM1ZWUiLCJpYXQiOjE3MTYxNjcxMTMsImV4cCI6MTcxNjE2ODAxM30.S65HZb2-iwws-3UgpKaIOxwmRfzAeHsDzrnZ75xHXYxpmjzOP_8zxg0adu0skV38PMuMRgvOnXzQCJIxGJTBF9YfB0lDEEbjXlCG8kfsgX0wc4PaB-0hsywuGspdOLMeniO8N85kNJ6VdtiWETCRe2WEcDOgcYFiMsiqLjzdjPQfrda2g8KGjbvSeIw3K6Qzm2i3YC0updVONjQg3sKX3KaUiHByggSl1lC6hPQgy3nX5Wj_FyJiJQaDQFtNsdR6Sh70ZX0cg6QM1yLGGxDHOnJvwUs4yifzuu_oOvDi-1kZVddH0ZNgCBGowLrgGAGkmPRg_yGIEb9RETnjeuV16A",
    })
    console.log("getMe response >>>", res)
  } catch (error) {
    console.error(error)
  }
}

async function verifyEmailCall() {
  try {
    const res = await verifyEmail({
      verification_code: "2e350381870352be9620e94a3b9a08912bbc0aff",
    })
    console.log("verify email response >>>", res)
  } catch (error) {
    console.error(error)
  }
}

// 生成一测试邮箱账号及密码
// async function createTestMailAccount() {
//   const credentials = await nodemailer.createTestAccount()
//   console.log("🚀 ~ createTestMailAccount ~ credentials:", credentials)
// }
// createTestMailAccount()
